package vsy.buzzwordbingo.client;

import java.awt.Frame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.io.IOException;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.web.client.RestTemplate;

import vsy.buzzwordbingo.model.Game;
import vsy.buzzwordbingo.model.impl.GameBuilder;
import vsy.buzzwordbingo.model.impl.GameImpl;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JButton;

public class ServerList extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTextArea serverlisteTextArea = null;
	private JLabel jLabel = null;
	private JComboBox serverListenBox = null;
	private JLabel neuesSpiel = null;
	private JLabel titel = null;
	private JLabel spielfeldgroesse = null;
	private JLabel Beschreibung = null;
	private JTextArea beschreibungsArea = null;
	private JTextField fieldtitle = null;
	private JTextField fieldsize = null;
	private JButton weiterButton = null;
	private JPanel jPanel1 = null;
	private JLabel nameLabel = null;
	
	/**
	 * This is the default constructor
	 */
	public ServerList() {
		super();
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public  void initialize() {
		this.setSize(new Dimension(800, 640));
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		//this.setLayout(null);
		
		this.setTitle("Buzzword Bingo");
		
		Beschreibung = new JLabel();
		Beschreibung.setBounds(new Rectangle(104, 255, 119, 18));
		Beschreibung.setText("Beschreibung:");
		spielfeldgroesse = new JLabel();
		spielfeldgroesse.setBounds(new Rectangle(104, 225, 119, 15));
		spielfeldgroesse.setText("Spielfeldgroesse:");
		titel = new JLabel();
		titel.setBounds(new Rectangle(104, 195, 112, 16));
		titel.setText("Titel:");
		neuesSpiel = new JLabel();
		neuesSpiel.setBounds(new Rectangle(369, 134, 116, 17));
		neuesSpiel.setText("Neues Spiel");
		jLabel = new JLabel();
		jLabel.setText("Serverliste");
		jLabel.setBounds(new Rectangle(369, 35, 62, 16));
		
		this.setContentPane(getJPanel1());
		
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.out.println("Client Closed"); 
				System.exit(0);
			}
		});
	}

	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new BackGroundPane("src/main/images/buzzwordbingo_serverlist_bg.jpg");
			jPanel1.setLayout(null);
			jPanel1.add(jLabel, null);
			jPanel1.add(getServerListenBox(), null);
			jPanel1.add(neuesSpiel, null);
			jPanel1.add(titel, null);
			jPanel1.add(spielfeldgroesse, null);
			jPanel1.add(Beschreibung, null);
			jPanel1.add(getBeschreibungsArea(), null);
			jPanel1.add(getFieldtitle(), null);
			jPanel1.add(getFieldsize(), null);
			jPanel1.add(getWeiterButton(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes serverListenBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 * 
	 */
	private JComboBox getServerListenBox() {
		if (serverListenBox == null) {
			serverListenBox = new JComboBox();
			serverListenBox.setBounds(new Rectangle(99, 60, 613, 20));
			
			serverListenBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				//	System.out.println(serverListenBox.getSelectedItem() + " wurde ausgew�hlt.");	
				}
			});
			
			String serverList = new RestTemplate().getForObject("http://localhost:8080/buzzwordbingo/json/games/", String.class);
			ObjectMapper mapper = new ObjectMapper();
			try {
				List<GameBuilder> games = mapper.readValue(serverList, new TypeReference<List<GameBuilder>>() { });
				for (GameBuilder g : games) {
					serverListenBox.addItem(g.createGame().getId());
				}
				// serverlisteTextArea.setText(buffer);
			} catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		
		return serverListenBox;
	}

	/**
	 * This method initializes beschreibungsArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getBeschreibungsArea() {
		if (beschreibungsArea == null) {
			beschreibungsArea = new JTextArea();
			beschreibungsArea.setBounds(new Rectangle(255, 255, 285, 134));
		}
		return beschreibungsArea;
	}

	/**
	 * This method initializes fieldtitle	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFieldtitle() {
		if (fieldtitle == null) {
			fieldtitle = new JTextField();
			fieldtitle.setBounds(new Rectangle(255, 195, 286, 18));
		}
		return fieldtitle;
	}

	/**
	 * This method initializes fieldsize	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFieldsize() {
		if (fieldsize == null) {
			fieldsize = new JTextField();
			fieldsize.setBounds(new Rectangle(255, 225, 286, 18));
		}
		return fieldsize;
	}

	/**
	 * This method initializes weiterButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getWeiterButton() {
		if (weiterButton == null) {
			weiterButton = new JButton();
			weiterButton.setBounds(new Rectangle(550, 364, 91, 26));
			weiterButton.setText("weiter");
		}
		return weiterButton;
	}

}  //  @jve:decl-index=0:visual-constraint="178,0"
